<?php
require_once dirname(__FILE__) . '/libs/phpMailer/class.phpmailer.php';

class EmailForward {
  	public static function forward_booked($email) {
  		global $myPdo;
  		$cond_vals = new stdClass();
	    $cond_vals->c = 't.id = :v1 AND t.status = :v2';
	    $cond_vals->v = array(':v1' => $email[1], ':v2' => 1);
  		$opts = new stdClass();
        $opts->select_expr = 't.id';
        $enqs = DbUtils::get($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_enquiry', $cond_vals, 't.date1', 'ASC', NULL, NULL, $opts)->d;
    
	  	if (count($enqs) > 0) {
	  		$cond_vals = new stdClass();
	        $cond_vals->c = 't.attr1 = :v1 AND t.parent_id = :v2';
	        $cond_vals->v = array(':v1' => $email[0], ':v2' => 0);
	        $messages = DbUtils::get($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_email_messages', $cond_vals, 't.id', 'DESC', NULL, NULL, NULL)->d;
	  		
	  		if (count($messages) > 0) {
	  			$subject = $messages[0]->attr3;
	  			$content = $messages[0]->attr4;
	  			$attachs = array();
	  			$cond_vals = new stdClass();
		        $cond_vals->c = 't.id = :v1';
		        $cond_vals->v = array(':v1' => $email[0]);
		        $emails = DbUtils::get($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_emails', $cond_vals, 't.date1', 'ASC', NULL, NULL, NULL)->d;
	  			
	  			$cond_vals = new stdClass();
		        $cond_vals->c = 't.message_id = :v1 AND t.email_id = :v2';
		        $cond_vals->v = array(':v1' => $messages[0]->id, ':v2' => $email[0]);
		        $attachs = DbUtils::get($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_email_message_attachments', $cond_vals, NULL, 'ASC', NULL, NULL, NULL)->d;
		  		
		  	    $tmp = DbUtils::get($myPdo, 'pz_profile', NULL, NULL, NULL, NULL, NULL, NULL)->d;
		  		if (count($tmp) > 0) {
				    $mailer = new PHPMailer();
				    $mailer->CharSet = "UTF-8";
				    
				    $mailer->IsSMTP();
				    $mailer->Host = $tmp[0]->mail_host;
				    
				    $subject = "Booked Customer (" . Server::customerId($enqs[0]->id) . ") From The Enquiry System"; //$subject;
				    $content = $content;
				    $mail_from = (count($emails) > 0) ? $emails[0]->email : $tmp[0]->mail_from_mail;
	  				$mail_from_name = (count($emails) > 0) ? $emails[0]->name : $tmp[0]->mail_from_name;
				    
				    $mailer->SMTPAuth = $tmp[0]->mail_SMTPAuth;
				    $mailer->Username = $tmp[0]->mail_account;
				    $mailer->Password = $tmp[0]->mail_password;
				    $mailer->From = $tmp[0]->mail_from_mail;
				    $mailer->FromName = $tmp[0]->mail_from_name;
				    $mailer->Body = $content;
				    $mailer->Subject = $subject;
				  
				    $mailer->isHTML(true);
				    $mailer->AddAddress(SHOP_EMAIL, SHOP_EMAIL_NAME);
				    $mailer->AddReplyTo($mail_from, $mail_from_name);   //回复地址
				    
				    foreach ($attachs as $attach) {
				        $path =  "attaches/" . $attach->name_on_server;
				        $mailer->AddAttachment($path, $attach->name);
				    }
				    
				    if (!$mailer->Send()) {
				      die('send failed');
				    }
			  	}
	  		}
	  	}
  	}
}
?>